package com.iMusic.presentationLayer;

import java.io.IOException;
import java.sql.SQLException;
import java.util.Enumeration;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.iMusic.businessLogicLayer.Inventory;
import com.iMusic.businessLogicLayer.Order;
import com.iMusic.businessLogicLayer.Product;
import com.iMusic.util.UserMessages;

public class ManageInventory extends HttpServlet {
	
	
	public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

		HttpSession session = request.getSession();
		String redirection = "adminFS.jsp?action=ManageInventory";
		List<Product> prodInventory = null;
		boolean deleteProduct = false;

		session.removeAttribute("Error");

		Integer prodId = -1;
		String name = null;
		String description = null;
		String type = null;
		String category = null;
		Float price = (float) 0.0;
		Integer uis = -1;

		Enumeration<String> parameterNames = request.getParameterNames();

		while (parameterNames.hasMoreElements()) {
			String paramName = parameterNames.nextElement();
			String prodIdStr = paramName.substring(paramName.indexOf('_')+1);
			if (prodIdStr != null && !prodIdStr.equals("")) {
				prodId = Integer.parseInt(prodIdStr);
			}
			
			if (paramName.startsWith("DeleteProduct_")) {
				deleteProduct = true;
				break;
			}
			if (paramName.startsWith("Name_")) {
				name = request.getParameter(paramName);
			}
			if (paramName.startsWith("Description_")) {
				description = request.getParameter(paramName);
			}
			if (paramName.startsWith("Type_")) {
				type = request.getParameter(paramName);
			}
			if (paramName.startsWith("Category_")) {
				category = request.getParameter(paramName);
			}
			if (paramName.startsWith("Price_")) {
				price = Float.parseFloat(request.getParameter(paramName));
			}
			if (paramName.startsWith("UnitsInStock_")) {
				uis = Integer.parseInt(request.getParameter(paramName));
			}
		}			
			

		try {
			if (deleteProduct) {
				Inventory.getInstance().deleteProduct(prodId);
			}
			else {
				Product prod = new Product();
				prod.setProductId(prodId);
				prod.setName(name);
				prod.setDescription(description);
				prod.setTypeName(type);
				prod.setCategoryName(category);
				prod.setPrice(price);
				prod.setUnitsInStock(uis);
				
				if (prodId > 0) {
					Inventory.getInstance().updateProduct(prod);
				}
				else {
					Inventory.getInstance().addNewProduct(prod);
				}
			}
		} catch (SQLException e) {
			e.printStackTrace();
			session.setAttribute("Error", UserMessages.INTERNAL_DB_ERROR);
			redirection += "?error="+UserMessages.INTERNAL_DB_ERROR;
		}
		catch (Exception e) {
			e.printStackTrace();
			session.setAttribute("Error", UserMessages.UNKNOWN_ERROR);
			redirection += "?error="+UserMessages.UNKNOWN_ERROR;
		}
		
		response.sendRedirect(redirection);
	}

}
